package com.thkj.service.impl;

import com.thkj.dao.DevicesMapper;
import com.thkj.dao.DisinfectRecordMapper;
import com.thkj.entity.Device;
import com.thkj.service.DisinfectRecordService;
import com.thkj.framework.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DisinfectRecordServiceImpl extends BaseService implements DisinfectRecordService {

    //消毒时间标准
    private static final int standard = 2;


    @Autowired
    DevicesMapper devicesMapper;

    @Autowired
    DisinfectRecordMapper disinfectRecordMapper;


    @Override
    public List<Map> queryDisinfectStatistics(String individual_id, int type, String timeStr) {
        List<Map> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        Map<String, Object> map4 = new HashMap<>();
        Map<String, Object> map5 = new HashMap<>();

        Device device = new Device();
        device.setIndividualId(individual_id);
        device.setDeviceType(1);
        List<Device> devices = devicesMapper.select(device);

        int deviceCount = 0;
        if (devices != null) {
            deviceCount = devices.size();
        }
        List<Map> list1 = new ArrayList();
        if (type == 1) {  //年
            list1 = devicesMapper.queryDisinfectYearByIndividualId(individual_id, timeStr);
        } else if (type == 2) {  //月
            list1 = devicesMapper.queryDisinfectMonthByIndividualId(individual_id, timeStr);
        } else if (type == 3) {  //天
            list1 = devicesMapper.queryDisinfectDayByIndividualId(individual_id, timeStr);
        } else if (type == 4) { //本周
            list1 = devicesMapper.queryDisinfectWeekByIndividualId(individual_id, timeStr);

        }
        Double durationCount = 0d;

        for (Map map : list1) {
            Double duration = (Double) map.get("duration");
            if (duration != null) {
                durationCount += duration;
            }
        }

        Integer[] arr = getqualified(type, devices, timeStr);

        map1.put("设备总数(" + deviceCount + ")", deviceCount);
        map2.put("消毒总时长(" + durationCount + ")", durationCount);
        map5.put("总检查天数(" + (arr[0] + arr[1]) + ")", (arr[0] + arr[1]));
        map3.put("合格总数(" + arr[0] + ")", arr[0]);
        map4.put("不合格总数(" + arr[1] + ")", arr[1]);


        list.add(map1);
        list.add(map2);
        list.add(map5);
        list.add(map3);
        list.add(map4);
        return list;
    }

    public Integer[] getqualified(int type, List<Device> devices, String timeStr) {
        Integer[] arr = new Integer[2];
        int i0 = 0;
        int i1 = 0;

        for (Device device : devices) {
            List<Double> doubles = null;
            if (type == 1) {  //年
                doubles = disinfectRecordMapper.querySumYearGroupByWorkerId(device.getId(), timeStr);
            } else if (type == 2) {  //月
                doubles = disinfectRecordMapper.querySumMonthGroupByWorkerId(device.getId(), timeStr);
            } else if (type == 3) {  //天
                doubles = disinfectRecordMapper.querySumDayGroupByWorkerId(device.getId(), timeStr);
            } else if (type == 4) { //本周
                doubles = disinfectRecordMapper.querySumWeekGroupByWorkerId(device.getId(), timeStr);
            }
            if (doubles != null) {
                for (Double aDouble : doubles) {
                    if (aDouble >= 2) {
                        ++i0;
                    } else {
                        ++i1;
                    }
                }
            }
        }
        arr[0] = i0;
        arr[1] = i1;
        return arr;
    }

}